package adventure.commands.admin;

import adventure.*;
import adventure.commands.*;
import colour.ColourUtil;
import player.*;

/**
 *
 * @author Michael Hanns
 *
 */
public class Immortal extends AbstractCommand {

    public Immortal(Adventure a) {
        super(a, new String[]{"IMMORTAL"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (p.isAdmin()) {
            if (args.length == 1) {
                return toggleImmortal(p);
            } else if (args.length == 2) {
                return toggleImmortal(p, args[1]);
            } else {
                return "Command not recognised.";
            }
        } else {
            return "Command not recognised.";
        }
    }

    private String toggleImmortal(Player p) {
        if (((PlayerEdit) p).isImmortal()) {
            return "You are currently "
                    + ColourUtil.colourise("immortal", ColourUtil.white)
                    + ".";
        }
        return "You are currently "
                + ColourUtil.colourise("mortal", ColourUtil.white)
                + ".";
    }

    private String toggleImmortal(Player p, String arg) {
        boolean immortal;
        if (arg.equalsIgnoreCase("on")) {
            immortal = true;
        } else if (arg.equalsIgnoreCase("off")) {
            immortal = false;
        } else {
            return "Set immortal on/off?\nSyntax: immortal <on/off>";
        }

        ((PlayerEdit)p).setImmortal(immortal);
        
        return toggleImmortal(p);
    }
}
